﻿using EM.Components.DataAccess;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using EM.Components.Entities;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace EM.Components.DataAccess.Tests
{
    
    
    /// <summary>
    ///This is a test class for LeadsDataAccessTest and is intended
    ///to contain all LeadsDataAccessTest Unit Tests
    ///</summary>
    [TestClass()]
    public class LeadsDataAccessTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Add
        ///</summary>
        [TestMethod()]
        public void AddTest()
        {
            LeadsDataAccess target = new LeadsDataAccess(); // TODO: Initialize to an appropriate value
            EM_Leads entity = null; // TODO: Initialize to an appropriate value
            entity = new EM_Leads();
            entity.FirstName = "Wei";
            entity.LastName = "Li";
            // tostring.replace can be used for replacing any char with sth needed.
            entity.EmailAddress = DateTime.Now.ToString().Replace(" ","").Replace("/","").Replace(":","") + "test@test.com";
            entity.IsValid = null;
            entity.Unsubscribed = null;
           // EM_Leads expected = null; // TODO: Initialize to an appropriate value
            EM_Leads actual;
            actual = target.Add(entity);
            Assert.AreEqual(true,(actual!=null && actual.LeadID>0));
        } 

        /// <summary>
        ///A test for GetAll
        ///</summary>
        [TestMethod()]
        public void GetAllTest()
        {
            EMEntities em = new EMEntities(ConfigurationManager.ConnectionStrings["EMEntities"].ConnectionString);
            LeadsDataAccess target = new LeadsDataAccess(); // TODO: Initialize to an appropriate value
            List<EM_Leads> expected = (from l in em.EM_Leads
                                       orderby l.LeadID
                                       select l).ToList<EM_Leads>();// TODO: Initialize to an appropriate value
            List<EM_Leads> actual;
            actual = target.GetAll();
            Assert.AreEqual(actual.Count,expected.Count);
        }

        /// <summary>
        ///A test for Get
        ///</summary>
        [TestMethod()]
        public void GetTest()
        {
            LeadsDataAccess target = new LeadsDataAccess(); // TODO: Initialize to an appropriate value
            EM_Leads actual;
            actual = target.Get(4);
            Assert.AreEqual(true,(actual!=null && actual.LeadID==4));
        }

        /// <summary>
        ///A test for Update
        ///</summary>
        [TestMethod()]
        public void UpdateTest()
        {
            LeadsDataAccess target = new LeadsDataAccess(); // TODO: Initialize to an appropriate value
            EM_Leads entity = target.Get(4); // TODO: Initialize to an appropriate value
            entity.LastName = "Lin";
            entity.FirstName = "Ruifang";
            EM_Leads actual=target.Update(entity);
            EM_Leads expected = target.Get(4);
            Assert.AreEqual(true,(expected.LastName=="Lin" && expected.FirstName=="Ruifang"));
        }

        /// <summary>
        ///A test for Get
        ///</summary>
        [TestMethod()]
        public void GetTest1()
        {
            LeadsDataAccess target = new LeadsDataAccess(); // TODO: Initialize to an appropriate value
            string email = string.Empty; // TODO: Initialize to an appropriate value
            email = "0509201211801PMtest@test.com";
            EM_Leads actual;
            actual = target.Get(email);
            Assert.AreEqual(true, (actual != null && actual.EmailAddress == "0509201211801PMtest@test.com"));
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod()]
        public void DeleteTest()
        {
            LeadsDataAccess target = new LeadsDataAccess(); // TODO: Initialize to an appropriate value
            int id = 12;
            EM_Leads entity = target.Get(id); // TODO: Initialize to an appropriate value
            target.Delete(entity);
            Assert.IsNull(target.Get(id));

        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod()]
        public void DeleteTest1()
        {
            LeadsDataAccess target = new LeadsDataAccess(); // TODO: Initialize to an appropriate value
            int entityId = 10; // TODO: Initialize to an appropriate value
            target.Delete(entityId);
            Assert.IsNull(target.Get(entityId));
        }
    }
}
